package org.ssmd3.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ssmd3.pojo.NewsType;
import org.ssmd3.service.NewsTypeService;
import org.ssmd3.util.PageTool;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/newstype")
    public class NewsTypeController {
    @Resource
    NewsTypeService newsTypeService;


    @RequestMapping("/dodeleteall")
    @ResponseBody
    public String dodeleteAll(String ids){
        String[] idsArray=ids.split(",");
            try {
              newsTypeService.deleteAll(idsArray);
                    return "1";

            }catch (Exception e){
                e.printStackTrace();
                    return "0";
            }
    }

@RequestMapping("/save")
    public String save(Model model){
        List<NewsType> plist=newsTypeService.getTopNewsTypeList();
        model.addAttribute("plist",plist);

        return "/newstype/save1";
    }
//@RequestMapping("/dosave")
//    public String doSave(Model model, NewsType newsType){
//
//        if (newsTypeService.save(newsType)>0){
//            model.addAttribute("label","alert('新增数据成功！')");
//        }
////        List<NewsType> plist=newsTypeService.getTopNewsTypeList();
////        model.addAttribute("plist",plist);
//
//        return "/newstype/save";
//    }

    @RequestMapping("/dosave")
    public String doSave(Model model, NewsType newsType){

        newsTypeService.save(newsType);

        return "redirect:/newstype/save";
    }
@RequestMapping("/update")
    public String update(Model model,int id){
        NewsType newsType=newsTypeService.getOneNewsType(id);
        List<NewsType> plist=newsTypeService.getTopNewsTypeList();
        model.addAttribute("plist",plist);
        model.addAttribute("newsType",newsType);

        return "/newstype/update";
    }

@RequestMapping("/doupdate")
    public String doUpdate(Model model,NewsType newsType){

        if (newsTypeService.update(newsType)>0){
            model.addAttribute("label","alert('修改数据成功');window.location='/newstype/list'");

        }else {
            model.addAttribute("label","alert('修改数据失败');window.location='/newstype/update?id="+newsType.getId()+"';");

        }
        return "/success";
}
    @RequestMapping("/dodelete")
    @ResponseBody
    public String doDelete(Model model,int id){
        try {
            newsTypeService.delete(id);
                return "1";

        }catch (Exception e){
            return "0";
        }

    }
@RequestMapping("/list")
    public String list(Model model, NewsType newsType){

        int totalRecordCount=newsTypeService.getRecordCount(newsType);

        PageTool pageTool=new PageTool(totalRecordCount,newsType.getCurrPageNo(),2);

        int starNum=pageTool.getStartRow();
        int pageSize=pageTool.getPageSize();
        newsType.setStarNum(starNum);
        newsType.setPageSize(pageSize);

        List<NewsType> list=newsTypeService.getNewsTypeListPage(newsType);
        List<NewsType> plist=newsTypeService.getTopNewsTypeList();
        model.addAttribute("list",list);
        model.addAttribute("plist",plist);
        model.addAttribute("newsType",newsType);
        model.addAttribute("pageTool",pageTool);
        model.addAttribute("prePages",pageTool.getPrePages());
        model.addAttribute("nextPages",pageTool.getNextPages());


        return "/newstype/list";
    }


}
